<?php defined('SYSPATH') or die('No direct script access.');
  
class Controller_Cms_Pages extends Controller_Cms_Setbackend
{
	private $model;
	
	public function before(){
		parent::before();
		$this->model = new Model_Cms_Pages;
    }
	
	public function action_pagelist($id=null)
	{
		$view = View::factory('cms/pages_list');
		if($id == null)
		{
			$this->template->breadcrumb = html::anchor('admin','Start')." > Lista stron";
			$pages = $this->model->get_all_pages();
			$view->category = 0;
		}else{
			$sup = $this->model->get_page($id);
			$this->template->breadcrumb = html::anchor('admin','Start')." > ".html::anchor('admin/pages/'.$id,$sup['title']);
			$pages = $this->model->get_category_pages($id);
			$view->category = $id;
		}
		$view->pages = $pages;
		$this->template->content = $view;
	}
	
	public function action_addpage($id=0,$cat=0)
	{
		$view = View::factory('cms/pages_add');
		$view->id = $id;
		
		if($id != 0){
			$page = $this->model->get_page($id);
			$view->page = $page;
			$view->cat_check = 0;
			$sup = $this->model->get_page($page['category_id']);
			$this->template->breadcrumb = html::anchor('admin','Start')." > ".html::anchor('admin/pages/'.$page['category_id'],$sup['title'])." > ".html::anchor('admin/addpage/'.$page['id'],$page['title']);
		}else{
			$sup = $this->model->get_page($cat);
			// jeĹĽeli dodawanie strony do kategorii - przekazanie do widoku odpowiedniej wartoĹ›ci zmiennej
			if($cat != 0)
				$view->cat_check = $cat;
				else $view->cat_check = 0;
			$view->page = null;
			$this->template->breadcrumb = html::anchor('admin','Start')." > ".html::anchor('admin/pages/'.$cat,$sup['title'])." > Nowa strona";
		}
		
		if($_POST)
		{
			//echo Kohana::debug($_POST);exit;			
			if($id == 0)
			{
				$cat_return = $this->model->add_page($_POST);
				Session::instance()->set('flash_panel','<div class="flash-ok">Strona została dodana</div>');
				Request::instance()->redirect('admin/pages/'.$cat_return);
			}else{
				$this->model->update_page($id,$_POST);
				Session::instance()->set('flash_panel','<div class="flash-ok">Dane zostały zmienione</div>');
				Request::instance()->redirect('admin/addpage/'.$id);
			}
		}
		$this->template->content = $view;
	}
	
	public function action_delpage($id)
	{
		$this->model->del_page($id);
		Request::instance()->redirect('admin/pages/4');
	}
	
	public function action_delthumb($id)
	{
		$news = $this->model->get_page($id);
		if($news['excerpt_photo'] != '')
		{
			$file_del = $_SERVER['DOCUMENT_ROOT'].Kohana::$base_url."/upload/news/".$news['excerpt_photo'];
			$file_del = str_replace("//","/",$file_del);
			if(file_exists($file_del))
			{
				unlink($file_del);
			}
		}
		$this->model->save_excerpt_photo($id,"");
		Request::instance()->redirect('admin/addpage/'.$id);
	}
	
	/* Categories [?] */
	
	public function action_categorylist()
	{
		$view = View::factory('cms/category_list');
		$this->template->breadcrumb = html::anchor('admin','Start')." > Lista kategorii";
		$categories = $this->model->get_all_categories();
		$view->categories = $categories;
		$this->template->content = $view;
	}
}
?>